/*
 * Copyright 2015-2016 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.webull.information.center.carwler.dao;

import java.util.List;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import com.webull.framework.util.UtilJdbc;
import com.webull.information.center.carwler.common.model.ArchiveRel;
import com.webull.information.center.carwler.common.model.TickerChain;

/**
 * 
 * @Description
 * @author smj
 * @Date 下午2:37:14
 * @version jdk1.8
 */
@Repository
public class ArchiveDao {
	private static final RowMapper<ArchiveRel> archiveRelRM = new BeanPropertyRowMapper<>(ArchiveRel.class);
	private static final RowMapper<TickerChain> tickerChainRM = new BeanPropertyRowMapper<>(TickerChain.class);
	protected final Logger logger = LogManager.getLogger(getClass());

	private @Autowired @Qualifier("wl_data_jdbcTemplate") JdbcTemplate jdbcTemplate;
	private @Autowired @Qualifier("wl_data_npJdbcTemplate") NamedParameterJdbcTemplate npJdbcTemplate;

	public ArchiveRel getArchiveRel(Integer type, Integer wlId) {
		return UtilJdbc.queryForObject(jdbcTemplate, Sql.getArchiveRel, archiveRelRM, type, wlId);
	}

	public ArchiveRel getArchiveRelBySrcId(String src, String srcId) {
		String sql = null;
		if (ArchiveRel.Src.rt_data_ric.equals(src)) {
			sql = "select * from wld_archive_rel where " + src + "=? limit 1";
		} else {
			sql = "select * from wld_archive_rel where " + src + "_id=? limit 1";
		}
		return UtilJdbc.queryForObject(jdbcTemplate, sql, archiveRelRM, srcId);
	}

	/*
	 * for newsGui
	 */
	public List<ArchiveRel> queryArchivesBySrcId(String srcId) {
		String sql = "select * from wld_archive_rel " //
				+ "where (" + ArchiveRel.Src.rt_nric + "_id= ? or "//
				+ ArchiveRel.Src.rt_ric + "_id= ? )"//
				+ " and type in (1,2,3)";
		return jdbcTemplate.query(sql, archiveRelRM, srcId, srcId);
	}

	public static void main(String[] args) {
		String sql = "select * from wld_archive_rel " //
				+ "where (" + ArchiveRel.Src.rt_nric + "_id= ? or "//
				+ ArchiveRel.Src.rt_ric + "_id= ? )"//
				+ " and type in (1,2,3)";
		System.out.println(sql);
	}

	public List<TickerChain> getTickerChainList(Integer type) {
		String sql = "select * from wld_ticker_chain where type = ? and del = 0";
		return jdbcTemplate.query(sql, tickerChainRM, type);
	}

	public static final class Sql {
		private static final String getArchiveRel = "select * from wld_archive_rel where type=? and wl_id=?";
	}

}
